<template>
  <div class="table-container">
    <vab-query-form>
      <!--      <vab-query-form-right-panel>-->
      <el-form
        ref="form"
        :model="replyForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="ID">
          <el-input v-model="replyForm.id" clearable placeholder="回复ID" />
        </el-form-item>

        <el-form-item label="父评论ID">
          <el-input
            v-model="replyForm.commentId"
            clearable
            placeholder="父评论ID"
          />
        </el-form-item>

        <el-form-item label="回复用户ID">
          <el-input
            v-model="replyForm.fromUid"
            clearable
            placeholder="内容ID"
          />
        </el-form-item>

        <el-form-item label="回复用户ID">
          <el-input
            v-model="replyForm.fromId"
            clearable
            placeholder="回复用户ID"
          />
        </el-form-item>

        <el-form-item label="目标用户ID">
          <el-input
            v-model="replyForm.toUId"
            clearable
            placeholder="目标用户ID"
          />
        </el-form-item>

        <el-form-item label="发布时间">
          <el-date-picker
            v-model="replyForm.createTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 200px"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <!--      </vab-query-form-right-panel>-->
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
        header-align="center"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="序号"
        width="95"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column> -->

      <el-table-column
        show-overflow-tooltip
        prop="id"
        label="ID"
        width="150"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="replyId"
        label="父评论ID"
        width="150"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="fromUid"
        label="回复用户ID"
        width="200"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="toUid"
        label="目标用户ID"
        width="200"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="content"
        label="内容"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="发布时间"
        header-align="center"
        :formatter="timeFormat"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
        width="180px"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <!-- <el-button type="text" @click="handleEdit(row)">编辑</el-button> -->
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="replyForm.page"
      :layout="layout"
      :page-size="replyForm.size"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { replySelectBy } from '@/api/management/management'
  //时间格式化
  import { shortTime, time } from '@/utils/timeFormat'
  export default {
    name: 'ReplyManagement',
    components: {},
    data() {
      return {
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        replyForm: {
          page: 1,
          size: 20,
        },
        classificationList: {},
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      tableSortChange() {
        const imageList = []
        this.$refs.tableSort.tableData.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await replyDeleteByIds([row.id]).then((res) => {
              if (res.code == 200) {
                this.$baseMessage(res.data, 'success')
                this.fetchData()
              } else {
                this.$baseMessage('删除失败', 'error')
              }
            })
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              await replyDeleteByIds(ids).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                  this.fetchData()
                } else {
                  this.$baseMessage('删除失败', 'error')
                }
              })
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.replyForm.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.replyForm.page = val
        this.fetchData()
      },
      handleQuery() {
        this.replyForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        await replySelectBy(this.replyForm).then((res) => {
          if (res.code === 200) {
            this.list = res.data
            const imageList = []
            //总数，决定页数
            this.total = 50
          } else {
            this.$baseMessage('数据获取失败', 'error')
          }
        })
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      //格式化时间
      timeFormat(row) {
        return time(row.createTime)
      },
    },
  }
</script>
